package com.kgcx.coupon.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.coupon.domain.CouponType;
import com.kgcx.coupon.mapper.CouponTypeMapper;
import com.kgcx.dbutils.domain.BaseDomain;
import com.kgcx.dbutils.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CouponTypeService {

    @Autowired
    private CouponTypeMapper couponTypeMapper;


    public CouponType getCouponType(Long kid) {
        return couponTypeMapper.selectByPrimaryKey(kid);
    }

    public List<CouponType> getCouponTypeList(RequestData rd) {
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        return couponTypeMapper.selectActive(BaseDomain.buildSelectDomain(rd, new CouponType()));
    }

    @Transactional(rollbackFor = Exception.class)
    public void addCouponType(CouponType couponType) {
        couponType.setOrderType(couponType.getOrderType());
        couponTypeMapper.insertSelective(couponType);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateCouponType(CouponType couponType) {
        couponTypeMapper.updateByPrimaryKeySelective(couponType);
    }


    @Transactional(rollbackFor = Exception.class)
    public void removeCouponType(Long kid) {
        CouponType couponType = new CouponType();
        couponType.setKid(kid);
        couponType.setStatus(Cons.COMMON_STATUS_DELETE);
        couponTypeMapper.updateByPrimaryKeySelective(couponType);
    }

}
